﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuotationSoftware.DialogForm
{
    public partial class CreateMasterPlanSimple : Form
    {
        public CreateMasterPlanSimple()
        {
            InitializeComponent();

            Global.mProject = new Business.Project();
        }

        private void bt_MasterPlan_AddNew_Click(object sender, EventArgs e)
        {
            //check input data
            int checkValue = CheckProjectInputData(dE_StartDate.DateTime, dE_EndDate.DateTime, tb_MasterPlanName.Text);
            switch (checkValue)
            {
                case 1:
                    MessageBox.Show(Common.MESSAGE_STRING.STARTDATE_GREATER_THAN_ENDDATE, Common.MESSAGE_TITLE.ERROR);
                    break;
                case 2:
                    MessageBox.Show(Common.MESSAGE_STRING.NULLTEXT_ERROR, Common.MESSAGE_TITLE.ERROR);
                    break;
                default:
                    //valid data
                    Global.mProject.ProgramID = (long)SLK_Program.EditValue;
                    Global.mProject.ProjectCode = tb_MasterPlanCode.Text;
                    Global.mProject.ProjectName = tb_MasterPlanName.Text;
                    Global.mProject.DefaultMargin = (double)sE_margin.Value;
                    Global.mProject.StartDate = dE_StartDate.DateTime;
                    Global.mProject.EndDate = dE_EndDate.DateTime;

                    if (Global.mProject != null)
                    {
                        //update
                        Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE errCode = Business.Project.Update(Global.mProject, Global._UserInfo);
                        //show message
                        MessageBox.Show(Common.EnumUtils.stringValueOf(errCode));
                        break;
                    }
                    else
                    {
                        //add new masterplan
                        Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW errCode = Business.Project.AddNew(ref Global.mProject, Global._UserInfo);
                        //show message
                        MessageBox.Show(Common.EnumUtils.stringValueOf(errCode));
                        break;
                    }

            }
        }

        private int CheckProjectInputData(DateTime startdate, DateTime enddate, string MasterPlanName)
        {
            if (MasterPlanName.Trim().Equals("")
                || startdate.Equals(DateTime.MinValue)
                || enddate.Equals(DateTime.MinValue))
            {
                return 2;
            }
            if (startdate.CompareTo(enddate) >= 0)
            {
                return 1;
            }
            return 0;
        }
    }
}
